package com.dys.test09;

/**
 * @className: com.dys.test09.Student
 * @description: com.dys.test09
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/07/23 下午9:23
 */
public class Student {
    // 封装属性
    private String name;
    private int age;
    private String gender;

    public Student() {
    }

    public Student(String name, int age, String gender) throws Exception {
        this.name = name;
        this.age = age;
        this.setGender(gender);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) throws Exception {
        // 方式1: 判断
        if ("男".equals(gender) || "女".equals(gender)) {
            this.gender = gender;
        } else {
            // 1.什么都不做
            // 2.打印信息
            // System.out.println("性别有误!");
            // 3.给默认值
            // this.gender = "男";
            // 4抛出运行时异常
            // throw new RuntimeException("性别有误!");
            // 5.主动制造检查异常
            // try {
            //     throw new Exception();
            // } catch (Exception e) {
            //     e.printStackTrace();
            // }
            // 6.往外抛
            throw new Exception();
        }

    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}
